package bug.hibernate;

import org.hibernate.cfg.ImprovedNamingStrategy;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.StringUtils;

public class MyNamingStrategy extends ImprovedNamingStrategy {
    private String[] uppercaseColumns;
    private String uppercaseColumnNames;

    @Override
    public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
        return columnName(propertyName) + "_id";
    }

/*
    @Override
    public String classToTableName(String className) {
        return "h" + className.substring(0, 1).toUpperCase() + pluralize(super.classToTableName(className)).substring(1);
    }
*/

/*
    @Override
    public String logicalColumnName(String columnName, String propertyName) {

        if (ArrayUtils.contains(uppercaseColumns, columnName))
            return super.logicalColumnName(StringUtils.upperCase(columnName), propertyName);

        return super.logicalColumnName(columnName, propertyName);
    }
*/

    private String pluralize(String name) {
        StringBuilder p = new StringBuilder(name);
        if (name.endsWith("y")) {
            p.deleteCharAt(p.length() - 1);
            p.append("ies");
        } else if (name.endsWith("ss")) {
            p.append("es");
        } else {
            p.append("s");
        }
        return p.toString();
    }

/*
    @Required
    public String getUppercaseColumnNames() {
        return uppercaseColumnNames;
    }

    @Required
    public void setUppercaseColumnNames(String uppercaseColumnNames) {
        this.uppercaseColumnNames = uppercaseColumnNames;
        this.uppercaseColumns = StringUtils.split(uppercaseColumnNames, ',');
    }
*/

}



